package com.example.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment
        .app.FragmentTransaction;
import androidx.fragment.app.FragmentManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // 找图片
       ImageView img1 =findViewById(R.id.img_main);
       ImageView img2=findViewById(R.id.img_phon);
       ImageView img3=findViewById(R.id.img_find);
       ImageView img4=findViewById(R.id.img_my);

       // 点击事件
        img1.setOnClickListener(l);
        img2.setOnClickListener(l);
        img3.setOnClickListener(l);
        img4.setOnClickListener(l);

    }
    View.OnClickListener l=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            // 通过fragmentmanager找到fragment
            FragmentTransaction ft = fm.beginTransaction(); // 作用：创建一个事务 用来 添加一个fragment   移除 fragment 创建 fragment
            // 使用if
            Fragment f = null;
            if(v.getId()==R.id.img_main){
                 f=new BlankFragment();

            }else if(v.getId()==R.id.img_phon){
                 f=new BlankFragment2();

            }else if(v.getId()==R.id.img_find){
                 f=new BlankFragment3();

            }else if(v.getId()==R.id.img_my){
                 f=new BlankFragment4();

            }
//            ft.replace(R.id.fragment,f);
//            ft.commit();
            // 跳转 到fragment
            fm.beginTransaction().replace(R.id.fragment,f).commit();


        }
    };
}